package br.com.puc.controllers;

import java.util.List;

import br.com.caelum.vraptor.Delete;
import br.com.caelum.vraptor.Get;
import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Put;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.Validator;
import br.com.puc.models.AlunoPosGraduacao;
import br.com.puc.models.annotations.Publico;
import br.com.puc.repositories.AlunoPosGraduacaoRepository;
import br.com.puc.repositories.ProfessorRepository;

@Resource
public class AlunoPosGraduacaoController {

	private final Result result;
	private final AlunoPosGraduacaoRepository repository;
	private final ProfessorRepository professorRepository;
	
	private final Validator validator;
	
	public AlunoPosGraduacaoController(Result result, AlunoPosGraduacaoRepository repository, ProfessorRepository professorRepository, 
	Validator validator) {
		this.result = result;
		this.repository = repository;
		this.professorRepository = professorRepository;
		this.validator = validator;
	}

	@Publico
	@Get("/alunosPosGraduacao")
	public List<AlunoPosGraduacao> index() {
		return repository.findAll();
	}
	
	@Post("/alunosPosGraduacao")
	public void create(AlunoPosGraduacao alunoPosGraduacao) {
		validator.validate(alunoPosGraduacao);
		validator.onErrorUsePageOf(this).newAlunoPosGraduacao();
		repository.create(alunoPosGraduacao);
		result.redirectTo(this).index();
	}
	
	@Get("/alunosPosGraduacao/new")
	public AlunoPosGraduacao newAlunoPosGraduacao() {
		result.include("professores", professorRepository.findAll());
		return new AlunoPosGraduacao();
	}
	
	@Put("/alunosPosGraduacao")
	public void update(AlunoPosGraduacao alunoPosGraduacao) {
		validator.validate(alunoPosGraduacao);
		result.include("professores", professorRepository.findAll());
		validator.onErrorUsePageOf(this).edit(alunoPosGraduacao);
		repository.update(alunoPosGraduacao);
		result.redirectTo(this).index();
	}
	
	@Get("/alunosPosGraduacao/{alunoPosGraduacao.id}/edit")
	public AlunoPosGraduacao edit(AlunoPosGraduacao alunoPosGraduacao) {
		result.include("professores", professorRepository.findAll());
		return repository.find(alunoPosGraduacao.getId());
	}

	@Publico
	@Get("/alunosPosGraduacao/{alunoPosGraduacao.id}")
	public AlunoPosGraduacao show(AlunoPosGraduacao alunoPosGraduacao) {
		return repository.find(alunoPosGraduacao.getId());
	}

	@Delete("/alunosPosGraduacao/{alunoPosGraduacao.id}")
	public void destroy(AlunoPosGraduacao alunoPosGraduacao) {
		repository.destroy(repository.find(alunoPosGraduacao.getId()));
		result.redirectTo(this).index();  
	}
}